package ${gen.packageName}.${gen.subPackageName}.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import ${gen.packageName}.${gen.subPackageName}.model.${entity.MODEL_NAME};
import ${gen.packageName}.${gen.subPackageName}.model.json.${entity.MODEL_NAME}Json;
import ${gen.packageName}.${gen.subPackageName}.service.${entity.MODEL_NAME}Service;

import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;

import com.msteam.kernel.controller.BaseController;
import com.msteam.kernel.render.vo.GridGson;
import com.msteam.kernel.render.vo.ResponseJson;
import com.msteam.kernel.router.RouterMapping;

@RouterMapping(url = "/desktop/${gen.subPackageName}/${entity.SUB_MODEL_NAME?lower_case}", viewPath = "/desktop/${gen.subPackageName}/${entity.SUB_MODEL_NAME?lower_case}")
public class ${entity.MODEL_NAME}Controller extends BaseController{
	
	/**
	 * 首页
	 */
	public void index(){
		String status = this.getPara("status","all");
		if(status.equalsIgnoreCase("all")){
			Page<${entity.MODEL_NAME}> page = ${entity.MODEL_NAME}Service.me().paginate(getPageNumber(), getPageSize(), getPara("order","ID"));
			setAttr("page", page);
		}else{
			//Page<${entity.MODEL_NAME}> page = ${entity.MODEL_NAME}Service.me().paginateByStatus(status, getPageNumber(), getPageSize(), getPara("order","ID"));
			Page<${entity.MODEL_NAME}> page = ${entity.MODEL_NAME}Service.me().paginate(getPageNumber(), getPageSize(), getPara("order","ID"));
			setAttr("page", page);
		}
		this.setAttr("status", status);
		this.ExtraRender("index.html");
	}
	
	/**
	 * 列表
	 */
	public void list(){

		Page<${entity.MODEL_NAME}> page = ${entity.MODEL_NAME}Service.me().paginate(getPageNumber(), getPageSize(), getPara("order"));
		
		List<${entity.MODEL_NAME}Json> jsonItems = _wrapJson(page.getList());
		if(this.JsonMode()){
			GridGson gg = new GridGson();	
			gg.setTotal(jsonItems.size());
			gg.setRows(jsonItems);
			String datas = gson.toJson(gg);
			this.renderJson(datas);
		}else{
			setAttr("page", page);
			this.ExtraRender("list.html");
		}
	}

	/**
	 * 查看
	 */
	public void view(){
		${entity.MODEL_NAME}Json jsonItem = getJsonItem();
		if(this.JsonMode()){
			this.renderJson(gson.toJson(jsonItem));
		}else{
			setAttr("item", jsonItem);
			this.ExtraRender("view.html");
		}		
	}

	/**
	 * 编辑
	 */
	public void edit(){
		${entity.MODEL_NAME}Json jsonItem = getJsonItem();
		if(this.JsonMode()){
			this.renderJson(gson.toJson(jsonItem));
		}else{
			setAttr("item", jsonItem);
			this.ExtraRender("edit.html");
		}		
	}

	/**
	 * 删除
	 */
	public void remove(){
		${entity.MODEL_NAME} item = getItem();
		item.set("DEL_FLAG", true);
		item.save();
		this.list();
	}

	/**
	 * 更新
	 */
	public void update(){
		String[] validAttrs = new String[]{
			"ID"
			<#list entity.fields as field>
			,"${field.NAME?upper_case}" // ${field.DISPLAY_NAME}
			</#list>
		};
		Map t = this.getPostParamModel(validAttrs);
		${entity.MODEL_NAME} item = null;
		if(t.containsKey("ID")){
			item = new ${entity.MODEL_NAME}().findById(t.get("ID"));
			item._setAttrs(t);
			item.update();
		}else{
			item = new ${entity.MODEL_NAME}();
			item._setAttrs(t);
			item.save();
		}
		if(this.JsonMode()){
			this.renderJson(gson.toJson(ResponseJson.SuccessResult()));
		}else{
			${entity.MODEL_NAME}Json jsonItem = _wrapJsonItem(item);
			setAttr("item", jsonItem);
			this.ExtraRender("edit.html");
		}
	}
	
	private List<${entity.MODEL_NAME}Json> _wrapJson(List<${entity.MODEL_NAME}> items){
		List<${entity.MODEL_NAME}Json> jsonItems = new ArrayList<${entity.MODEL_NAME}Json>();
		for(${entity.MODEL_NAME} gt:items){
			jsonItems.add(_wrapJsonItem(gt));
		}
		return jsonItems;
	}

	private ${entity.MODEL_NAME}Json _wrapJsonItem(${entity.MODEL_NAME} objItem){
		List<${entity.MODEL_NAME}Json> jsonItems = new ArrayList<${entity.MODEL_NAME}Json>();
		
		${entity.MODEL_NAME}Json jsonItem = new ${entity.MODEL_NAME}Json();
		<#list entity.fields as field>
		jsonItem.set${field.NAME?upper_case}(objItem.get${field.ATTR_NAME?cap_first}()); // ${field.DISPLAY_NAME}
		</#list>

		return jsonItem;
	}
	private ${entity.MODEL_NAME}Json getJsonItem(){
		${entity.MODEL_NAME}Json jsonItem = _wrapJsonItem(getItem());
		return jsonItem;
	}
	private ${entity.MODEL_NAME} getItem(){
		${entity.MODEL_NAME} item = null;
		if(StrKit.isBlank(this.getPara("id"))){
			item = new ${entity.MODEL_NAME}();
		}else{
			Integer id = this.getParaToInt("id");
			item = new ${entity.MODEL_NAME}().findById(id);
		}
		return item;
	}
	
}